package com.tb.ClassSchedule;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * Created by guich on 2016/8/21.
 */
public class WeekSelector extends AlertDialog.Builder implements NumberPicker.Formatter,NumberPicker.OnValueChangeListener{

    private NumberPicker startWeekPicker;
    private NumberPicker endWeekPicker;
    private String[] data = { "第1周" , "第2周" , "第3周" , "第4周" , "第5周" , "第6周" , "第7周" , "第8周" , "第9周" , "第10周" , "第11周" , "第12周" , "第13周" , "第14周" , "第15周" , "第16周" , "第17周" , "第18周" , "第19周" , "第20周"};


    private RadioGroup radioGroup ;


    public WeekSelector(Context context) {
        super(context);
        setTitle("周数选择");
        View contentView = LayoutInflater.from(context).inflate(R.layout.select_week,null);
        setView(contentView);

        radioGroup = (RadioGroup) contentView.findViewById(R.id.radio_group);
        ((RadioButton)radioGroup.getChildAt(2)).setChecked(true);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                Log.d("123", String.valueOf(radioGroup.getCheckedRadioButtonId()));


            }
        });
        startWeekPicker = (NumberPicker) contentView.findViewById(R.id.start_picker);
        endWeekPicker = (NumberPicker) contentView.findViewById(R.id.end_picker);

        startWeekPicker.setDisplayedValues(data);
        startWeekPicker.setOnValueChangedListener(this);
        startWeekPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        startWeekPicker.setMinValue(1);
        startWeekPicker.setMaxValue(20);

        endWeekPicker.setDisplayedValues(data);
        endWeekPicker.setOnValueChangedListener(this);
        endWeekPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        endWeekPicker.setMinValue(1);
        endWeekPicker.setMaxValue(20);

        setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                AddClassActivity.startWeek = startWeekPicker.getValue();
                AddClassActivity.endWeek = endWeekPicker.getValue();
                Log.d(getClass().getName(),"" + startWeekPicker.getValue() + endWeekPicker.getValue() + " : " + radioGroup.getCheckedRadioButtonId());

                String str = "";
                str += (" 第" + startWeekPicker.getValue());
                if(startWeekPicker.getValue() == endWeekPicker.getValue()){
                    str += ("周");
                }else{
                    str += (" ~ " + endWeekPicker.getValue() + "周");
                }
                AddClassActivity.selectWeek.setText(str);


            }
        });


        setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });

    }

    public WeekSelector(Context context,int start,int end){
        this(context);
        startWeekPicker.setValue(start);
        endWeekPicker.setValue(end);
    }


    @Override
    public String format(int i) {
        return "第" + i + "周";
    }

    @Override
    public void onValueChange(NumberPicker numberPicker, int i, int i1) {
        if(endWeekPicker.getValue() < startWeekPicker.getValue()){
            endWeekPicker.setValue(startWeekPicker.getValue());
        }
    }
}
